﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GestionEco;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace Ecosystème
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        #region variables game

        private GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;
        private float[] tailleImages;
        private List<TypeEspece> listTypeEspece;
        private int opacite = 255;
        private Form f;
        PanneauDeControle pannelControle;
        private frmGestionPlateau pannelGestionPlateau;
        private frmAjout pannelAjout;
        #endregion

        #region variables gestion du temps

        private float timer = 10; 
        const float TIMER = 0.1F;

        #endregion

        #region Variables gestion des controles
        private MouseState posSouris;
        private KeyboardState curState;
        private Vector2 positionDuClic;
        private float translationX = 0.0F;
        private float translationY = 0.0F;
        #endregion

        #region Variables de la 3D

            private readonly Vector2 scale = new Vector2(1.0f, 0.5f);
            private readonly Vector2 OrigineDeLaMap = new Vector2(-(TileCount * TileSize) / 2.0f, (TileCount * TileSize) / 2.0f);
            private const int TileCount = 5;
            private const int TileSize = 200;
            readonly float angleDeRotation = 0.7854f;
            private RenderTarget2D mapEn2D;
            private RenderTarget2D mapTransfo;

        #endregion

        public Game1()
        {
            tailleImages = new float[2];
            tailleImages[0] = 50;
            tailleImages[1] = 50;
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            this.IsMouseVisible = true;
            IntPtr ptr = this.Window.Handle;
            f = (System.Windows.Forms.Form)System.Windows.Forms.Control.FromHandle(ptr);
            f.FormBorderStyle = FormBorderStyle.None;
           

        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            #region Initialisation
            pannelControle = new PanneauDeControle();
            pannelControle.StartPosition = FormStartPosition.Manual;
            pannelControle.Location = new System.Drawing.Point(1083, 84);
            pannelControle.FormBorderStyle = FormBorderStyle.None;
            pannelControle.Show();
            pannelGestionPlateau = new frmGestionPlateau();
            pannelGestionPlateau.FormBorderStyle = FormBorderStyle.None;
            pannelGestionPlateau.Location = new System.Drawing.Point(5, 0);
            pannelGestionPlateau.Quitter = false;
            pannelGestionPlateau.Reduire = false;
            pannelGestionPlateau.Play = false;
            pannelGestionPlateau.Show();
            listTypeEspece = new List<TypeEspece>();
            System.Drawing.Image img = System.Drawing.Image.FromFile(Content.RootDirectory + "/EtreVivants/Chevre.png");
            TypeEspece te = new TypeEspece("Chèvre", img);
            listTypeEspece.Add(te);
            img = System.Drawing.Image.FromFile(Content.RootDirectory + "/EtreVivants/Loup.png");
            te = new TypeEspece("Loup", img);
            listTypeEspece.Add(te);
            img = System.Drawing.Image.FromFile(Content.RootDirectory + "/EtreVivants/pigeon.png");
            te = new TypeEspece("Pigeon", img);
            listTypeEspece.Add(te);
            img = System.Drawing.Image.FromFile(Content.RootDirectory + "/EtreVivants/requin.png");
            te = new TypeEspece("Requin", img);
            listTypeEspece.Add(te);
            img = System.Drawing.Image.FromFile(Content.RootDirectory + "/EtreVivants/tulipe.png");
            te = new TypeEspece("Tulipe", img);
            listTypeEspece.Add(te);
            img = System.Drawing.Image.FromFile(Content.RootDirectory + "/EtreVivants/cactus.png");
            te = new TypeEspece("Cactus", img);
            listTypeEspece.Add(te);
            img = System.Drawing.Image.FromFile(Content.RootDirectory + "/EtreVivants/chat.png");
            te = new TypeEspece("Chat de gouttière", img);
            listTypeEspece.Add(te);
            img = System.Drawing.Image.FromFile(Content.RootDirectory + "/EtreVivants/chien.png");
            te = new TypeEspece("Chien sauvage", img);
            listTypeEspece.Add(te);
            img = System.Drawing.Image.FromFile(Content.RootDirectory + "/EtreVivants/chameau.png");
            te = new TypeEspece("Chameau", img);
            listTypeEspece.Add(te);
            pannelAjout = new frmAjout();
            pannelAjout.StartPosition = FormStartPosition.Manual;
            pannelAjout.Location = new System.Drawing.Point(5, 84);
            pannelAjout.FormBorderStyle = FormBorderStyle.None;
            pannelAjout.listtype = listTypeEspece;
            pannelAjout.Show();
            base.Initialize();
            #endregion
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Traitements.initialisation(this.Content);

            #region ConvertionImages
            convertionDesImages();
            #endregion

            #region Génération des maps pour 2D/3D
                mapEn2D = new RenderTarget2D(GraphicsDevice, TileCount * TileSize, TileCount * TileSize);
                mapTransfo = new RenderTarget2D(GraphicsDevice, 2 * (TileCount * TileSize), 2 * (TileCount * TileSize));
            #endregion

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
                this.Exit();

            
            // TODO: Add your update logic here
            #region Gestion du clavier et souris

            curState = Keyboard.GetState();
            posSouris = Mouse.GetState();
            if (Traitements.lePlateau.EstEn3D)
            {
                f.Width = 1084;
            }
            else
            {
                f.Width = 800;
            }


            if (curState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Enter))
            {
                if (Traitements.lePlateau.EstEn3D)
                {
                    Traitements.lePlateau.EstEn3D = false;
                }
                else
                {
                    Traitements.lePlateau.EstEn3D = true;
                }
                Thread.Sleep(60);

            }

            if (curState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Z))
            {
                //On zoome
                Traitements.lePlateau.Zoom += 0.02F;

            }
            if (curState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.X))
            {
                // on dézoome
                //if (Traitements.lePlateau.Zoom > 1F)
                //{
                if (Traitements.taille[0] == 5)
                {
                    if ((Traitements.lePlateau.Zoom > 0.5000005F))
                    {
                        Traitements.lePlateau.Zoom -= 0.01F;
                    }
                }
                if (Traitements.taille[0] == 6)
                {
                    if ((Traitements.lePlateau.Zoom > 0.43F))
                    {
                        Traitements.lePlateau.Zoom -= 0.01F;
                    }
                }
                 if (Traitements.taille[0] == 7)
                {
                    if ((Traitements.lePlateau.Zoom > 0.37F))
                    {
                        Traitements.lePlateau.Zoom -= 0.01F;
                    }
                }
                if (Traitements.taille[0] == 8)
                {
                    if ((Traitements.lePlateau.Zoom > 0.33F))
                    {
                        Traitements.lePlateau.Zoom -= 0.01F;
                    }
                }
                 if (Traitements.taille[0] == 9)
                {
                    if ((Traitements.lePlateau.Zoom > 0.29F))
                    {
                        Traitements.lePlateau.Zoom -= 0.01F;
                    }
                }
                if (Traitements.taille[0] == 10)
                {
                    if ((Traitements.lePlateau.Zoom > 0.250005931F))
                    {
                        Traitements.lePlateau.Zoom -= 0.01F;
                    }
                }
                 if (Traitements.taille[0] >= 11)
                {
                    if ((Traitements.lePlateau.Zoom > 0.24F))
                    {
                        Traitements.lePlateau.Zoom -= 0.01F;
                    }
                }
            
            }
            if (pannelGestionPlateau.Reduire)
            {
                pannelGestionPlateau.WindowState = FormWindowState.Minimized;
                if (pannelControle != null)
                pannelControle.WindowState = FormWindowState.Minimized;
                f.WindowState = FormWindowState.Minimized;
                //if (fdc != null)
                //    fdc.WindowState = FormWindowState.Minimized;
                pannelGestionPlateau.Reduire = false;
            }
            if (f.WindowState == FormWindowState.Normal)
            {
                pannelGestionPlateau.WindowState = FormWindowState.Normal;
                if(pannelControle != null)
                pannelControle.WindowState = FormWindowState.Normal;
                //if (fdc != null)
                //    fdc.WindowState = FormWindowState.Normal;
            }
            if (pannelGestionPlateau.Quitter)
            {
                pannelGestionPlateau.Close();
                if (pannelControle != null)
                pannelControle.Close();
                //if (fdc != null)
                //    fdc.Close();
                this.Exit();
            }

            if (curState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Escape))
            {

                this.graphics.ToggleFullScreen();



            }
            if (curState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Right))
            {

                  translationX -= 10.0F * Traitements.lePlateau.Zoom;

            }

            if (curState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Left))
            {
                // On se déplace vers la gauche
                if (translationX < 0F)
                {
                    if ((translationX + (10.0F * Traitements.lePlateau.Zoom)) < 0F)
                    {
                        translationX += 10.0F * Traitements.lePlateau.Zoom;
                    }
                    else
                    {
                        translationX = 0F;
                    }
                }
               
            }
            if (curState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Down))
            {

                        translationY -= 10.0F * Traitements.lePlateau.Zoom;
            }
            if (curState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Up))
            {
                // On se déplace vers le haut
                if (translationY < 0F)
                {
                    if ((translationY + (10.0F * Traitements.lePlateau.Zoom)) < 0F)
                    {
                        translationY += 10.0F * Traitements.lePlateau.Zoom;
                    }
                    else
                    {
                        translationY = 0F;
                    }
                }
                
            }
            if (posSouris.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            {
                if (posSouris.X <= graphics.PreferredBackBufferWidth && posSouris.Y <= graphics.PreferredBackBufferHeight)
                {
                    positionDuClic = new Vector2((posSouris.X - translationX) / Traitements.lePlateau.Zoom, (posSouris.Y - translationY) / Traitements.lePlateau.Zoom);
                    opacite = 200;
                }
                Thread.Sleep(100);
            }
            if (posSouris.RightButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            {
                if (posSouris.X <= graphics.PreferredBackBufferWidth && posSouris.Y <= graphics.PreferredBackBufferHeight && posSouris.X >= 0 && posSouris.Y >= 0)
                {
                    positionDuClic = new Vector2(posSouris.X - translationX, posSouris.Y - translationY);

                    foreach (List<Case> C1 in Traitements.lePlateau.LesCases)
                    {

                        foreach (Case C2 in C1)
                        {
                            if ((positionDuClic.X > C2.Position.X) && (positionDuClic.X < (C2.Position.X + C2.TextureCase.Width)) && (positionDuClic.Y > C2.Position.Y) && (positionDuClic.Y < (C2.Position.Y + C2.TextureCase.Height)))
                            {
                                    opacite = 255;
                                    pannelControle.Close();
                                    pannelControle = new PanneauDeControle();
                                    pannelControle.StartPosition = FormStartPosition.Manual;
                                    pannelControle.Location = new System.Drawing.Point(1083, 84);
                                    pannelControle.FormBorderStyle = FormBorderStyle.None;
                                    pannelControle.Show();
                            }
                        }
                    }
                }
                Thread.Sleep(100);

            }
            #endregion

            #region Correction translation
            if (!Traitements.lePlateau.EstEn3D)
            {
                if (translationX < ((Traitements.lePlateau.LesCases[0][0].TextureCase.Width * (Traitements.lePlateau.LesCases.Count - (2 / Traitements.lePlateau.Zoom)) * Traitements.lePlateau.Zoom) * -1))
                {

                    translationX = (Traitements.lePlateau.LesCases[0][0].TextureCase.Width * (Traitements.lePlateau.LesCases.Count - (2 / Traitements.lePlateau.Zoom)) * Traitements.lePlateau.Zoom) * -1;

                }
                if (translationY < (Traitements.lePlateau.LesCases[0][0].TextureCase.Width * (Traitements.lePlateau.LesCases.Count - (1.5F / Traitements.lePlateau.Zoom)) * Traitements.lePlateau.Zoom) * -1)
                {

                    translationY = (Traitements.lePlateau.LesCases[0][0].TextureCase.Width * (Traitements.lePlateau.LesCases.Count - (1.5F / Traitements.lePlateau.Zoom)) * Traitements.lePlateau.Zoom) * -1;
                }
            }
            else
            {
                if (translationX < ((Traitements.lePlateau.LesCases[0][0].TextureCase.Width * (Traitements.lePlateau.LesCases.Count - (2.5F / Traitements.lePlateau.Zoom)) * Traitements.lePlateau.Zoom) * -1))
                {

                    translationX = (Traitements.lePlateau.LesCases[0][0].TextureCase.Width * (Traitements.lePlateau.LesCases.Count - (2.5F / Traitements.lePlateau.Zoom)) * Traitements.lePlateau.Zoom) * -1;

                }
                if (translationY < (Traitements.lePlateau.LesCases[0][0].TextureCase.Width * (Traitements.lePlateau.LesCases.Count - (2.5F / Traitements.lePlateau.Zoom)) * Traitements.lePlateau.Zoom) * -1)
                {

                    translationY = (Traitements.lePlateau.LesCases[0][0].TextureCase.Width * (Traitements.lePlateau.LesCases.Count - (2.5F / Traitements.lePlateau.Zoom)) * Traitements.lePlateau.Zoom) * -1;
                }
            }
            #endregion

            #region actions des etre vivants

            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
            timer -= elapsed;
            if (timer<0)
            {
               if ((Traitements.laListeEcosysteme.Count != 0) && (Traitements.start))
                {
                     Traitements.actionEtreVivant();
                }
                timer = TIMER;
            }
            #endregion

         


            base.Update(gameTime);
            
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
             GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.White);
             if (Traitements.lePlateau.EstEn3D)
             {
                 GraphicsDevice.SetRenderTarget(mapEn2D);
             }
             spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullNone, null, matriceDeTranslation(GraphicsDevice));

             // TODO: Add your drawing code here
             #region Dessin des Etre et cases
                 //Les cases sont dessinées

                 float hauteur = 0;
                 float largeur = 0;

                 System.Console.WriteLine(positionDuClic.X.ToString() + " : " + positionDuClic.Y.ToString());
                
	             foreach (List<Case> C1 in Traitements.lePlateau.LesCases)
	             {
	                 hauteur = 0;
	                 foreach (Case C2 in C1)
	                 {
	                     
	                     //Création des coordonnées de la case
	                     C2.Position = new Vector2(largeur, hauteur);
                         
	                     //On dessine les cases
                         if ((positionDuClic.X > C2.Position.X) && (positionDuClic.X < (C2.Position.X + C2.TextureCase.Width)) && (positionDuClic.Y > C2.Position.Y) && (positionDuClic.Y < (C2.Position.Y + C2.TextureCase.Height)))
                         {
                             spriteBatch.Draw(C2.TextureCase, C2.Position, Microsoft.Xna.Framework.Color.FromNonPremultiplied(255, 255, 255, opacite));
                             pannelControle.LaCaseSelectionnee = C2;
                             pannelControle.chargementDuPanneau();
                         }
                         else
                         {
                             spriteBatch.Draw(C2.TextureCase, C2.Position, Microsoft.Xna.Framework.Color.White);

                         }
                         hauteur += C2.TextureCase.Height;
	                 }
                     largeur += C1.ElementAt(0).TextureCase.Width;
	             }

             spriteBatch.End();
            //Les etre vivants sont dessinés
            float rad = -0.424999744F;
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullNone, null, matriceDeTranslation(GraphicsDevice));
            float interieurCaseX = 0;
            float interieurCaseY = 0;
            List<String> dejaDessine= new List<String>();

            foreach (List<Case> C3 in Traitements.lePlateau.LesCases)
            {

                foreach (Case C4 in C3)
                {
                    int compteImageCase = 0;
                    foreach (EtreVivant ev in C4.ListEVCase)
                    {

                        if (dejaDessine.IndexOf(ev.GetType().ToString()) == -1)
                        {
                            if (compteImageCase == 0)
                                spriteBatch.Draw(ev.Texture, new Vector2(C4.Position.X + 0, C4.Position.Y + 0), null, Microsoft.Xna.Framework.Color.FromNonPremultiplied(255, 255, 255, 255), rad, new Vector2(-0, -60), 0.8F, SpriteEffects.None, 0);
                            if (compteImageCase == 1)
                            {
                                spriteBatch.Draw(ev.Texture, new Vector2(C4.Position.X + 0, C4.Position.Y + 100), null, Microsoft.Xna.Framework.Color.FromNonPremultiplied(255, 255, 255, 255), rad, new Vector2(-0, -60), 0.8F, SpriteEffects.None, 0);
                            }
                            if (compteImageCase == 2)
                                spriteBatch.Draw(ev.Texture, new Vector2(C4.Position.X + 100, C4.Position.Y + 0), null, Microsoft.Xna.Framework.Color.FromNonPremultiplied(255, 255, 255, 255), rad, new Vector2(-0, -60), 0.8F, SpriteEffects.None, 0);
                            if (compteImageCase == 3)
                                spriteBatch.Draw(ev.Texture, new Vector2(C4.Position.X + 100, C4.Position.Y + 100), null, Microsoft.Xna.Framework.Color.FromNonPremultiplied(255, 255, 255, 255), rad, new Vector2(-0, -60), 0.8F, SpriteEffects.None, 0);
                            if (compteImageCase == 4)
                                spriteBatch.Draw(ev.Texture, new Vector2(C4.Position.X + 200, C4.Position.Y + 0), null, Microsoft.Xna.Framework.Color.FromNonPremultiplied(255, 255, 255, 255), rad, new Vector2(-0, -110), 0.8F, SpriteEffects.None, 0);
                            if (compteImageCase == 5)
                                spriteBatch.Draw(ev.Texture, new Vector2(C4.Position.X + 200, C4.Position.Y + 100), null, Microsoft.Xna.Framework.Color.FromNonPremultiplied(255, 255, 255, 255), rad, new Vector2(-0, -110), 0.8F, SpriteEffects.None, 0);
                            if (compteImageCase == 6)
                                spriteBatch.Draw(ev.Texture, new Vector2(C4.Position.X + 0, C4.Position.Y + 200), null, Microsoft.Xna.Framework.Color.FromNonPremultiplied(255, 255, 255, 255), rad, new Vector2(-0, -110), 0.8F, SpriteEffects.None, 0);
                            if (compteImageCase == 7)
                                spriteBatch.Draw(ev.Texture, new Vector2(C4.Position.X + 100, C4.Position.Y + 200), null, Microsoft.Xna.Framework.Color.FromNonPremultiplied(255, 255, 255, 255), rad, new Vector2(-0, -110), 0.8F, SpriteEffects.None, 0);
                            if (compteImageCase == 8)
                                spriteBatch.Draw(ev.Texture, new Vector2(C4.Position.X + 200, C4.Position.Y + 200), null, Microsoft.Xna.Framework.Color.FromNonPremultiplied(255, 255, 255, 255), rad, new Vector2(-0, -110), 0.8F, SpriteEffects.None, 0);
                            //spriteBatch.Draw(ev.Texture, new Vector2(C4.Position.X + interieurCaseX, C4.Position.Y + interieurCaseY), null, Microsoft.Xna.Framework.Color.White, rad, new Vector2(-0, -60), 0.8F, SpriteEffects.None, 0);
                            interieurCaseX += ev.Texture.Width;
                            interieurCaseY += ev.Texture.Height;
                            dejaDessine.Add(ev.GetType().ToString());
                            if (compteImageCase < 8)
                                compteImageCase++;
                        }

                        ev.CasePosition = C4;
                    }

                    interieurCaseX = 0;
                    interieurCaseY = 0;
                    dejaDessine.Clear();
                    //Redimentionnement par rapport a la valeur du zoom
                    //C4.Position = C4.Position * Traitements.lePlateau.Zoom;
                }

            }
                 #endregion
            

            spriteBatch.End();

            #region passage En 3D
            if (Traitements.lePlateau.EstEn3D)
            {
                Vector2 vect3D;
                GraphicsDevice.SetRenderTarget(mapTransfo);
                GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.White);

                spriteBatch.Begin();
                spriteBatch.Draw(mapEn2D, new Vector2(0, 400), null, Microsoft.Xna.Framework.Color.White, angleDeRotation, OrigineDeLaMap, 0.9F, SpriteEffects.None, 0);
                spriteBatch.End();

                GraphicsDevice.SetRenderTarget(null);
                GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.White);

                spriteBatch.Begin();
                vect3D = new Vector2(0.63F, 0.25F);
                spriteBatch.Draw(mapTransfo, Vector2.Zero, null, Microsoft.Xna.Framework.Color.White, 0, Vector2.Zero, vect3D, SpriteEffects.None, 0);
                spriteBatch.End();
            }
            #endregion


            Traitements.start = true;
            base.Draw(gameTime);
        }

        public Matrix matriceDeTranslation(GraphicsDevice graphicsDevice)
        {
            float Tx = translationX;
            float Ty = translationY;
            Matrix translateToOrigin = Matrix.CreateTranslation(0, 0, 0);

            // On crée le zoom 
            Matrix zoomMatrix = Matrix.CreateScale(Traitements.lePlateau.Zoom, Traitements.lePlateau.Zoom, 0);
            // On crée la translation
            Microsoft.Xna.Framework.Matrix translateBackToPosition = Microsoft.Xna.Framework.Matrix.CreateTranslation(Tx, Ty, 0);

            //On redimentionne en fonction des valeur
            Microsoft.Xna.Framework.Matrix compositeMatrix = zoomMatrix * translateBackToPosition * translateToOrigin;

            // On renvoie la transformation
            return compositeMatrix;
        }

        public void convertionDesImages()
        {
            foreach (List<Case> C1 in Traitements.lePlateau.LesCases)
            {
                foreach (Case C2 in C1)
                {
                    System.Drawing.Image im = C2.ImageCase;
                    //On resize l'image
                    Bitmap img = new Bitmap(im, 400, 400);
                    using (MemoryStream s = new MemoryStream())
                    {

                        img.Save(s, System.Drawing.Imaging.ImageFormat.Png);
                        s.Seek(0, SeekOrigin.Begin);
                        // Ajout de l'image sur la texture de l'être vivant
                        C2.TextureCase = Texture2D.FromStream(this.spriteBatch.GraphicsDevice, s);
                    }
                }
            }
            foreach (EtreVivant e in Traitements.laListeEcosysteme)
            {
                System.Drawing.Image im = e.ImageEtre;
                //On resize l'image
                Bitmap img = new Bitmap(im, new Size(100, 100));
                using (MemoryStream s = new MemoryStream())
                {

                    img.Save(s, System.Drawing.Imaging.ImageFormat.Png);
                    s.Seek(0, SeekOrigin.Begin);
                    // Ajout de l'image sur la texture de l'être vivant
                    e.Texture = Texture2D.FromStream(this.spriteBatch.GraphicsDevice, s);
                }
            }
        }

    }
}
